home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FGL304B.ZIP;1 / MANUALS.ARJ / USER07.DOC < prev    next >
Encoding:
Text File  |  1994-01-24  |  51.3 KB  |  1,127 lines

  1. Chapter 7
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Character Display Routines
  8.  
  9. 128   Fastgraph User's Guide
  10.  
  11.  
  12.  
  13. Overview
  14.  
  15.      An important part of any program is the capability to display text or
  16. other characters on the screen.  Fastgraph supports two character sets:  the
  17. hardware or BIOS character set available with each video mode, and
  18. Fastgraph's own software character set for graphics video modes.
  19. Fastgraph/Light does not support the software character set.
  20.  
  21.      We'll begin this chapter with a review of character space, and then
  22. discuss the specifics about hardware and software characters.  At the end of
  23. the chapter, we'll briefly explain how to implement bit-mapped characters
  24. into a program.  To simplify things, the example programs presented in this
  25. chapter are mode-specific examples, and no testing is done to check if the
  26. video mode is available on the user's system.
  27.  
  28.  
  29. Character Space
  30.  
  31.      The coordinate system used for displaying hardware characters is called
  32. character space.  It is the only coordinate system available in text video
  33. modes, but it is a supplementary coordinate system you can use with either
  34. screen space or world space in graphics video modes.  Character space can be
  35. thought of as a grid of rows and columns, with each cell in the grid holding
  36. one character.  Each cell is identified by its unique (row,column) integer
  37. coordinates.  The rows and columns are numbered starting at zero; the origin
  38. is always the upper left corner of the screen.  For example, in the 80-column
  39. by 25-row video modes, the (row,column) coordinates of the screen corners are
  40. shown in the following diagram.
  41.  
  42.                             (0,0)           (0,79)
  43.  
  44.  
  45.                             (24,0)         (24,79)
  46.  
  47. The default number of rows and columns depends on the video mode, as shown in
  48. the following table.  For graphics modes, the table also includes the default
  49. width and height in pixels of a character cell.
  50.  
  51.                            Mode                 Char. Char.
  52.                           Number  Columns Rows  Width Height
  53.  
  54.                              0      40     25
  55.                              1      40     25
  56.                              2      80     25
  57.                              3      80     25
  58.                              4      40     25     8     8
  59.                              5      40     25     8     8
  60.                              6      80     25     8     8
  61.                              7      80     25
  62.                              9      40     25     8     8
  63.                             11      80     25     9    14
  64.                             12      40     25     8     8
  65.                             13      40     25     8     8
  66.  
  67.                                   Chapter 7:  Character Display Routines   129
  68.  
  69.  
  70.                             14      80     25     8     8
  71.                             15      80     25     8    14
  72.                             16      80     25     8    14
  73.                             17      80     30     8    16
  74.                             18      80     30     8    16
  75.                             19      40     25     8     8
  76.                             20      40     25     8     8
  77.                             21      40     50     8     8
  78.                             22      40     30     8     8
  79.                             23      40     60     8     8
  80.                             24      80     25     8    16
  81.                             25      80     30     8    16
  82.                             26      100    37     8    16
  83.                             27      128    48     8    16
  84.                             28      100    37     8    16
  85.                             29      128    48     8    16
  86.  
  87. Hardware Characters
  88.  
  89.      Hardware characters are available in all of Fastgraph's supported video
  90. modes.  As explained in Chapter 5, text mode characters have a display
  91. attribute that defines their foreground color, their background color, and
  92. whether or not they blink.  Graphics mode characters appear in a single
  93. color, as determined by the current color index.  Chapter 5 also explained
  94. how Fastgraph's fg_setattr and fg_setcolor routines define the attribute or
  95. color index in which subsequent hardware characters appear.
  96.  
  97.      It is obviously important to define the color or attribute for hardware
  98. characters, but it is equally important to define their location on the
  99. screen.  Fastgraph draws hardware characters at the position defined by the
  100. text cursor.  Like the graphics cursor, the text cursor is not a cursor in
  101. the true sense, but is simply a pair of character space (row,column)
  102. coordinates with a special meaning.  The fg_setmode routine sets the text
  103. cursor position to the character space coordinates (0,0), which of course is
  104. the upper left corner of the screen.2
  105.  
  106.      The Fastgraph routine fg_locate changes the text cursor position.  It
  107. has two integer arguments that specify the (row,column) character space
  108. coordinates of the new position.  The row values must be between 0 and one
  109. less than the number of character rows available.  The column values must be
  110. between 0 and one less than the number of character columns available.
  111.  
  112.      The fg_text routine is Fastgraph's basic character display routine.  It
  113. displays a string of hardware characters, starting at the text cursor
  114. position, using the current color attribute (for text modes) or color index
  115. (for graphics modes).  If the string reaches the last column in a row,
  116. fg_text will wrap the string to the first column of the next row.
  117. Additionally, fg_text leaves the cursor one column to the right of the last
  118. character displayed (or the first column of the next row if the last
  119. character appears at the end of a row).  This feature makes it possible for
  120. successive calls to fg_text to display adjacent strings.  The first argument
  121. ____________________
  122.  
  123.      (2) In reality there are eight  text cursors, one for each video page.
  124. The fg_setmode routine initializes each text cursor position to (0,0).  The
  125. next chapter describes this in more detail.
  126. 130   Fastgraph User's Guide
  127.  
  128. for the fg_text routine is a character string of arbitrary length, and the
  129. second argument is an integer value that specifies the number of characters
  130. to display from that string.
  131.  
  132.      Example 7-1 illustrates the use of the fg_locate and fg_text routines in
  133. the 80 by 25 color text mode (mode 3).  After establishing the video mode and
  134. making the BIOS cursor invisible, the program displays four strings with
  135. different attributes.  The attributes are selected using the fg_setattr
  136. routine, and the strings are displayed by the fg_text routine.  The first
  137. string appears in yellow (attributes 14,0,0) in the upper left corner of the
  138. screen; the fg_locate routine is not necessary because (0,0) is the default
  139. text cursor position established by fg_setmode.  The second string appears in
  140. light green (10,0,0) one space to the right of the first string.  Its
  141. position relies on the fact fg_text leaves the text cursor positioned one
  142. space to the right of the last character displayed (following the "w" of
  143. "yellow" in this case).  The leading space in " green" leaves a space between
  144. the first and second strings.  Similarly, the third string appears in
  145. blinking light red (12,0,1) one space to the right of the second string.
  146.  
  147.      The program then uses the fg_locate routine to move the text cursor to
  148. the lower left corner of the screen and displays the "Press any key" string.
  149. This string is displayed with a light red foreground against a gray
  150. background (12,7,0).  The extra spaces surrounding the string extend the
  151. background color one character position to the left and right and make the
  152. string more visually appealing.  Finally, once you press any key, the program
  153. restores the original video mode and screen attributes before returning to
  154. DOS.
  155.  
  156.                                  Example 7-1.
  157.  
  158.                       #include <fastgraf.h>
  159.                       void main(void);
  160.  
  161.                       void main()
  162.                       {
  163.                          int old_mode;
  164.  
  165.                          old_mode = fg_getmode();
  166.                          fg_setmode(3);
  167.                          fg_cursor(0);
  168.  
  169.                          fg_setattr(14,0,0);
  170.                          fg_text("yellow",6);
  171.  
  172.                          fg_setattr(10,0,0);
  173.                          fg_text(" green",6);
  174.  
  175.                          fg_setattr(12,0,1);
  176.                          fg_text(" blinking",9);
  177.  
  178.                          fg_setattr(12,7,0);
  179.                          fg_locate(24,0);
  180.                          fg_text(" Press any key. ",16);
  181.                          fg_waitkey();
  182.  
  183.                          fg_setmode(old_mode);
  184.                          fg_reset();
  185.                                   Chapter 7:  Character Display Routines   131
  186.  
  187.  
  188.                       }
  189.  
  190.      The fg_where routine retrieves the text cursor position in its two
  191. integer arguments.  This routine is not used as frequently as the fg_locate
  192. and fg_text routines because more often than not your program will know the
  193. text cursor position implicitly, or you'll know in advance the locations at
  194. which text will be displayed.  The fg_where routine takes two integer
  195. arguments passed as pointers (that is, by reference), and these two arguments
  196. respectively receive the text cursor's current row and column position.
  197.  
  198.      Example 7-2 produces the same results as example 7-1, but it does so a
  199. bit differently.  It uses its own routine, put_string, to display a string at
  200. a specified row and column.  The put_string routine simply calls fg_locate to
  201. establish the text cursor position and then calls fg_text to display the
  202. string.  Note the use of the C library function strlen to determine the
  203. string length passed to the fg_text routine.  Example 7-2 also uses the
  204. fg_where routine to retrieve the new text cursor positions, which are then
  205. passed to the put_string routine.
  206.  
  207.                                  Example 7-2.
  208.  
  209.                    #include <fastgraf.h>
  210.                    #include <string.h>
  211.                    void main(void);
  212.                    void put_string(char*,int,int);
  213.  
  214.                    void main()
  215.                    {
  216.                       int old_mode;
  217.                       int row, column;
  218.  
  219.                       old_mode = fg_getmode();
  220.                       fg_setmode(3);
  221.                       fg_cursor(0);
  222.  
  223.                       fg_setattr(14,0,0);
  224.                       put_string("yellow",0,0);
  225.  
  226.                       fg_setattr(10,0,0);
  227.                       fg_where(&row,&column);
  228.                       put_string("green",row,column+1);
  229.  
  230.                       fg_setattr(12,0,1);
  231.                       fg_where(&row,&column);
  232.                       put_string("blinking",row,column+1);
  233.  
  234.                       fg_setattr(12,7,0);
  235.                       put_string(" Press any key. ",24,0);
  236.                       fg_waitkey();
  237.  
  238.                       fg_setmode(old_mode);
  239.                       fg_reset();
  240.                    }
  241.  
  242.                    void put_string(string,row,column)
  243.                    char *string;
  244. 132   Fastgraph User's Guide
  245.  
  246.  
  247.                    int row, column;
  248.                    {
  249.                       fg_locate(row,column);
  250.                       fg_text(string,strlen(string));
  251.                    }
  252.  
  253.      Sometimes you may wish to change the display attribute of existing text,
  254. such as when creating a shadow around the edges of a pop-up window.  The
  255. Fastgraph routine fg_chgattr performs this function.  It applies the current
  256. text display attribute (as defined in the most recent call to fg_setattr or
  257. fg_setcolor) to a given number of characters, starting at the text cursor
  258. position.  It leaves the text cursor one column to the right of the last
  259. character changed (or the first column of the next row if the last character
  260. is at the end of a row).  The fg_chgattr routine's argument specifies the
  261. number of characters to change.  This routine has no effect in graphics video
  262. modes.
  263.  
  264.      The Fastgraph routine fg_chgtext performs somewhat the opposite function
  265. of fg_chgattr.  It displays new text but uses the display attributes already
  266. assigned to the character cells where the text will appear.  The fg_chgtext
  267. routine takes the same two arguments as the fg_text routine, displays the
  268. characters starting at the text cursor position, and leaves the cursor one
  269. column to the right of the last character displayed.  Like fg_chgattr,
  270. fg_chgtext has no effect in graphics video modes.
  271.  
  272.      Example 7-3 illustrates the fg_chgattr and fg_chgtext routines.  It runs
  273. in the 80-column color text mode (mode 3), but if we change the fg_setmode
  274. argument it also would run in the monochrome text mode (mode 7).  The program
  275. first displays the word "hello" in the upper left corner of the screen, using
  276. a gray foreground and black background attribute.  After waiting for a
  277. keystroke, the program calls fg_chgattr to make the word "hello" appear in
  278. reverse video (that is, a black foreground and gray background attribute).
  279. After a second keystroke, the program uses fg_chgtext to change the "h" of
  280. "hello" to upper case.  Following this, the program returns to DOS.
  281.  
  282.                                  Example 7-3.
  283.  
  284.                          #include <fastgraf.h>
  285.                          void main(void);
  286.  
  287.                          void main()
  288.                          {
  289.                             int old_mode;
  290.  
  291.                             old_mode = fg_getmode();
  292.                             fg_setmode(3);
  293.                             fg_cursor(0);
  294.  
  295.                             fg_setattr(7,0,0);
  296.                             fg_text("hello",5);
  297.                             fg_waitkey();
  298.  
  299.                             fg_locate(0,0);
  300.                             fg_setattr(0,7,0);
  301.                             fg_chgattr(5);
  302.                             fg_waitkey();
  303.                                   Chapter 7:  Character Display Routines   133
  304.  
  305.  
  306.  
  307.                             fg_locate(0,0);
  308.                             fg_chgtext("H",1);
  309.                             fg_waitkey();
  310.  
  311.                             fg_setmode(old_mode);
  312.                             fg_reset();
  313.                          }
  314.  
  315.  
  316.      You also can retrieve the character or attribute stored in a specific
  317. character cell.  The Fastgraph routine fg_getchar retrieves character values,
  318. while fg_getattr retrieves character attributes.  Both routines have two
  319. integer arguments that specify the (row,column) coordinates for the character
  320. cell of interest.  Example 7-4 uses fg_getchar and fg_getattr to obtain the
  321. character and attribute stored at row 24, column 0.  Just before the program
  322. exits, it displays these values.
  323.  
  324.                                  Example 7-4.
  325.  
  326.                      #include <fastgraf.h>
  327.                      #include <stdio.h>
  328.                      void main(void);
  329.  
  330.                      void main()
  331.                      {
  332.                         int attr, value;
  333.                         int old_mode;
  334.  
  335.                         old_mode = fg_getmode();
  336.                         fg_setmode(3);
  337.                         fg_cursor(0);
  338.  
  339.                         fg_setattr(9,7,0);
  340.                         fg_locate(24,0);
  341.                         fg_text("Test",4);
  342.                         value = fg_getchar(24,0);
  343.                         attr  = fg_getattr(24,0);
  344.                         fg_waitkey();
  345.  
  346.                         fg_setmode(old_mode);
  347.                         fg_reset();
  348.                         printf("%c %2.2X\n",value,attr);
  349.                      }
  350.  
  351.  
  352.      If you need to retrieve characters and attributes from a rectangular
  353. area, it's more efficient to use the fg_getimage routine (described in
  354. Chapter 10) than to call fg_getchar and fg_getattr repeatedly.
  355.  
  356.      Displaying hardware characters in graphics video modes is quite
  357. different from doing so in text modes.  Like text modes, we can still use
  358. fg_text to display strings in character space, which of course restricts the
  359. places where strings can appear.  Graphics modes offer the ability to display
  360. strings relative to any pixel, not just character cells.  The fg_print and
  361. fg_justify routines are provided for this purpose.  To compare the two
  362. 134   Fastgraph User's Guide
  363.  
  364.  
  365. methods of displaying strings in graphics modes, let's begin with an example
  366. of doing so with fg_text.
  367.  
  368.      Example 7-5 is similar to example 7-1, but it runs in the EGA enhanced
  369. graphics mode (mode 16) instead of a text mode.  In graphics modes, the
  370. fg_cursor routine has no effect, so we have omitted it from the program.
  371. Furthermore, characters cannot be displayed with a blinking attribute, so we
  372. have omitted the blinking characters (we could simulate blinking by
  373. repetitively displaying and erasing them, but that is beyond the scope of
  374. this example).  Because graphics mode characters only have a foreground
  375. color, we had to simulate the gray background of the "Press any key" string
  376. by first drawing a rectangle where that string appears.  The differences
  377. between examples 7-5 and 7-1 hold for any graphics video mode, not just mode
  378. 16.
  379.  
  380.                                  Example 7-5.
  381.  
  382.                       #include <fastgraf.h>
  383.                       void main(void);
  384.  
  385.                       void main()
  386.                       {
  387.                          int old_mode;
  388.  
  389.                          old_mode = fg_getmode();
  390.                          fg_setmode(16);
  391.  
  392.                          fg_setcolor(14);
  393.                          fg_text("yellow",6);
  394.  
  395.                          fg_setcolor(10);
  396.                          fg_text(" green",6);
  397.  
  398.                          fg_setcolor(7);
  399.                          fg_rect(0,127,336,349);
  400.                          fg_setcolor(12);
  401.                          fg_locate(24,0);
  402.                          fg_text(" Press any key. ",16);
  403.                          fg_waitkey();
  404.  
  405.                          fg_setmode(old_mode);
  406.                          fg_reset();
  407.                       }
  408.  
  409.      Now let's show how to display graphics mode strings using the more
  410. flexible screen space coordinate system.  The fg_print routine is identical
  411. to fg_text, but it displays a string relative to the graphics cursor position
  412. (that is, in screen space) rather than the character space position
  413. established with fg_locate.  By default, fg_print displays strings so their
  414. lower left corner is at the graphics cursor position.  The fg_justify routine
  415. lets you change this default justification.  Its two parameters, xjust and
  416. yjust, control the string positioning about the current graphics position, as
  417. summarized in the following table:
  418.  
  419.            value of       value of      horizontal      vertical
  420.              xjust          yjust      justification  justification
  421.                                   Chapter 7:  Character Display Routines   135
  422.  
  423.  
  424.               -1             -1            left           lower
  425.               -1              0            left          center
  426.               -1              1            left           upper
  427.                0             -1           center          lower
  428.                0              0           center         center
  429.                0              1           center          upper
  430.                1             -1            right          lower
  431.                1              0            right         center
  432.                1              1            right          upper
  433.  
  434. Any other justification values produce undefined results.  In the context of
  435. vertical justification, lower justification means the bottom of each
  436. character will be at the current graphics y position.  Upper justification
  437. means the top of each character will be at the graphics y position, while
  438. center justification means characters will be centered about the graphics y
  439. position.  Note that the default justification settings are xjust = -1 and
  440. yjust = -1.  The fg_print routine leaves the graphics cursor positioned just
  441. beyond the bottom right corner of the last character displayed.
  442.  
  443.      Example 7-6 illustrates the use of fg_print and fg_justify to display
  444. justified text in the VGA 640 by 480 16-color graphics mode (mode 18).  The
  445. first series of calls to fg_move, fg_justify, and fg_print display the string
  446. "Fastgraph" left justified, centered, and right justified against the top row
  447. of the screen.  The second series of such calls also displays the string in
  448. these positions, but each is centered vertically in the middle of the screen.
  449. The final series displays the strings against the bottom row of the screen.
  450. The nine calls to fg_justify in example 7-6 represent all possible
  451. justification settings for strings displayed with fg_print.
  452.  
  453.                                  Example 7-6.
  454.  
  455.                          #include <fastgraf.h>
  456.                          void main(void);
  457.  
  458.                          void main()
  459.                          {
  460.                             int old_mode;
  461.  
  462.                             old_mode = fg_getmode();
  463.                             fg_setmode(18);
  464.                             fg_setcolor(9);
  465.                             fg_fillpage();
  466.                             fg_setcolor(14);
  467.  
  468.                             fg_move(0,0);
  469.                             fg_justify(-1,1);
  470.                             fg_print("Fastgraph",9);
  471.                             fg_move(320,0);
  472.                             fg_justify(0,1);
  473.                             fg_print("Fastgraph",9);
  474.                             fg_move(639,0);
  475.                             fg_justify(1,1);
  476.                             fg_print("Fastgraph",9);
  477.  
  478.                             fg_move(0,240);
  479.                             fg_justify(-1,0);
  480. 136   Fastgraph User's Guide
  481.  
  482.                             fg_print("Fastgraph",9);
  483.                             fg_move(320,240);
  484.                             fg_justify(0,0);
  485.                             fg_print("Fastgraph",9);
  486.                             fg_move(639,240);
  487.                             fg_justify(1,0);
  488.                             fg_print("Fastgraph",9);
  489.  
  490.                             fg_move(0,479);
  491.                             fg_justify(-1,-1);
  492.                             fg_print("Fastgraph",9);
  493.                             fg_move(320,479);
  494.                             fg_justify(0,-1);
  495.                             fg_print("Fastgraph",9);
  496.                             fg_move(639,479);
  497.                             fg_justify(1,-1);
  498.                             fg_print("Fastgraph",9);
  499.                             fg_waitkey();
  500.  
  501.                             fg_setmode(old_mode);
  502.                             fg_reset();
  503.                          }
  504.  
  505.      Example 7-7 demonstrates a side effect that occurs when displaying
  506. characters in graphics modes.  This example uses the MCGA graphics mode (mode
  507. 19) and displays two character strings at the same location.  If we were to
  508. do this in a text mode, the first string would disappear once we displayed
  509. the second string.  In graphics modes, however, the portions of the first
  510. string not covered by characters from the second string are still visible.
  511. The reason for this may not be clear at first, but remember when we display
  512. characters in graphics modes, we aren't really displaying characters but
  513. merely a pixel representation of the characters.  Fastgraph has no way to
  514. distinguish such pixels from any other pixels, no matter if we use fg_text or
  515. fg_print for string display.
  516.  
  517.                                  Example 7-7.
  518.  
  519.                          #include <fastgraf.h>
  520.                          void main(void);
  521.  
  522.                          void main()
  523.                          {
  524.                             int old_mode;
  525.  
  526.                             old_mode = fg_getmode();
  527.                             fg_setmode(19);
  528.  
  529.                             fg_setcolor(14);
  530.                             fg_text("yellow",6);
  531.                             fg_locate(0,0);
  532.                             fg_setcolor(10);
  533.                             fg_text(" green",6);
  534.                             fg_waitkey();
  535.  
  536.                             fg_setmode(old_mode);
  537.                             fg_reset();
  538.                          }
  539.                                   Chapter 7:  Character Display Routines   137
  540.  
  541.  
  542.      To avoid this problem, the recommended procedure for displaying
  543. characters in graphics modes is to first erase the area where the text will
  544. appear.  The easiest way to do this is to use the fg_rect routine to draw a
  545. rectangle in the background color.  In example 7-7, we could do this by
  546. inserting the statements
  547.  
  548.  
  549.                               fg_setcolor(0);
  550.                               fg_rect(0,47,0,7);
  551.  
  552.  
  553. immediately before the call to fg_locate.  The parameters passed to the
  554. fg_rect routine represent the 48 by 8 pixel region that corresponds to the
  555. first six character cells of row 0 in the 320 by 200 graphics modes.
  556.  
  557.  
  558. Character Height
  559.  
  560.      In VGA and SVGA graphics modes (modes 17 to 29), it's possible to change
  561. the height of characters displayed with fg_print or fg_text.  By default,
  562. characters are 16 pixels high in the VGA and SVGA graphics modes (17, 18, 24
  563. to 29) and 8 pixels high in the MCGA and XVGA graphics modes (19 to 23).  The
  564. fg_fontsize routine lets you display characters from the BIOS 8x8, 8x14, or
  565. 8x16 fonts in any of these modes.  Its only parameter specifies the character
  566. height in pixels; it must be 8, 14, or 16.  If the character height is some
  567. other value, or if fg_fontsize is used in a video mode numbered 16 or less
  568. (that is, in a non-VGA mode), nothing happens.
  569.  
  570.      When we change the character height with fg_fontsize, the number of text
  571. rows on the screen changes accordingly.  The following table shows the number
  572. of text rows available in each supported video mode when using the different
  573. character sizes.  The values in boldface type represent the default character
  574. size and number of rows for that video mode.
  575.  
  576.                                  Mode No. of rows with
  577.                                 Number 8x8  8x14 8x16
  578.  
  579.                                   17    60   34   30
  580.                                   18    60   34   30
  581.                                   19    25   14   12
  582.                                   20    25   14   12
  583.                                   21    50   28   25
  584.                                   22    30   17   15
  585.                                   23    60   34   30
  586.                                   24    50   28   25
  587.                                   25    60   34   30
  588.                                   26    75   42   37
  589.                                   27    96   54   48
  590.                                   28    75   42   37
  591.                                   29    96   54   48
  592.  
  593.      Example 7-8 shows how to use fg_fontsize to activate the 8x8 character
  594. font in the 16-color 640 by 480 VGA graphics mode (mode 18).  In this mode,
  595. characters displayed with fg_print or fg_text are normally 16 pixels high,
  596. 138   Fastgraph User's Guide
  597.  
  598.  
  599. giving 30 character rows per screen.  When we use the 8x8 font, this
  600. increases to 60 rows because the characters are now half as tall as before.
  601. The example program uses fg_text to display the string "8x8 ROM font" 60
  602. times, once in each row.
  603.  
  604.                                  Example 7-8.
  605.  
  606.                      #include <fastgraf.h>
  607.                      void main(void);
  608.  
  609.                      void main()
  610.                      {
  611.                         int old_mode;
  612.                         int row;
  613.  
  614.                         old_mode = fg_getmode();
  615.                         fg_setmode(18);
  616.  
  617.                         fg_setcolor(9);
  618.                         fg_fillpage();
  619.                         fg_setcolor(15);
  620.                         fg_fontsize(8);
  621.  
  622.                         for (row = 0; row < 60; row++) {
  623.                            fg_locate(row,34);
  624.                            fg_text("8x8 ROM font",12);
  625.                            }
  626.                         fg_waitkey();
  627.  
  628.                         fg_setmode(old_mode);
  629.                         fg_reset();
  630.                      }
  631.  
  632.  
  633. Conversion Routines
  634.  
  635.      In Chapter 4 we introduced Fastgraph's routines for converting
  636. coordinates between character space and screen space.  In this section we'll
  637. review these routines and then present an example that uses some of them.
  638.  
  639.      The fg_xalpha and fg_yalpha routines convert screen space coordinates to
  640. character space.  The fg_xalpha routine converts a screen space x coordinate
  641. to the character space column that contains the coordinate.  Similarly, the
  642. fg_yalpha routine converts a screen space y coordinate to the character space
  643. row that contains the coordinate.
  644.  
  645.      The fg_xconvert and fg_yconvert routines convert character space
  646. coordinates to screen space.  The fg_xconvert routine converts a character
  647. space column to the screen space coordinate of its leftmost pixel.
  648. Similarly, the fg_yconvert routine converts a character space row to the
  649. screen space coordinate of its top (lowest-numbered) pixel.
  650.  
  651.      Example 7-5 demonstrated how to display characters in a graphics mode.
  652. Because characters do not have a background color in graphics modes, that
  653. example used fg_rect to simulate a background color by drawing a gray
  654. rectangle before displaying the text.  It was necessary to determine the
  655.                                   Chapter 7:  Character Display Routines   139
  656.  
  657.  
  658. screen coordinates of the character cells so we could pass the correct
  659. parameters to fg_rect.  By using the fg_xconvert and fg_yconvert routines, we
  660. can let Fastgraph calculate the required screen coordinates.  This method has
  661. the additional benefit of working in any graphics mode, whereas the
  662. coordinates passed to fg_rect in example 7-5 would only work properly in a
  663. 640 by 350 graphics mode.  Example 7-9 shows how we could extend example 7-5
  664. to use the fg_xconvert and fg_yconvert routines.
  665.  
  666.                                  Example 7-9.
  667.  
  668.                       #include <fastgraf.h>
  669.                       void main(void);
  670.  
  671.                       void main()
  672.                       {
  673.                          int old_mode;
  674.                          int minx, maxx, miny, maxy;
  675.  
  676.                          fg_old_mode = fg_getmode();
  677.                          fg_setmode(16);
  678.  
  679.                          fg_setcolor(14);
  680.                          fg_text("yellow",6);
  681.  
  682.                          fg_setcolor(10);
  683.                          fg_text(" green",6);
  684.  
  685.                          fg_setcolor(7);
  686.                          minx = fg_xconvert(0);
  687.                          maxx = fg_xconvert(16) - 1;
  688.                          miny = fg_yconvert(24);
  689.                          maxy = fg_yconvert(25) - 1;
  690.                          fg_rect(minx,maxx,miny,maxy);
  691.                          fg_setcolor(12);
  692.                          fg_locate(24,0);
  693.                          fg_text(" Press any key. ",16);
  694.                          fg_waitkey();
  695.  
  696.                          fg_setmode(old_mode);
  697.                          fg_reset();
  698.                       }
  699.  
  700.  
  701.  
  702. Software Characters
  703.  
  704.      Software characters, also called stroke characters or vector characters
  705. in other literature, are only are available in graphics video modes.  Unlike
  706. the fixed-size hardware characters, you can display software characters in
  707. any size, at any angle, and at any position.  In addition, software
  708. characters are proportionally spaced.  However, software characters take
  709. longer to draw than do hardware characters.
  710.  
  711.      Fastgraph includes two software character fonts, called the primary font
  712. and the alternate font.  The primary font contains upper and lower case
  713. letters, numbers, punctuation, and most of the other printable ASCII
  714. 140   Fastgraph User's Guide
  715.  
  716. characters.  The alternate font contains upper and lower case Greek letters
  717. and other mathematical and scientific symbols.
  718.  
  719.      The Fastgraph routine fg_swchar displays a string of software characters
  720. in the current color index (as defined by the most recent call to
  721. fg_setcolor).  The string may contain any characters from the primary font,
  722. the alternate font, or both.  You can display the characters left justified,
  723. centered, or right justified relative to the graphics cursor position.  Just
  724. as the fg_text routine updates the text cursor position, fg_swchar sets the
  725. graphics cursor position just to the right of the last character drawn.  The
  726. characters are clipped according to the current clipping region.  In addition
  727. to the characters, the string passed to fg_swchar also may contain operators
  728. for switching fonts, underlining, subscripting, or superscripting characters.
  729. Because fg_swchar internally uses world space coordinates, you must call the
  730. fg_initw routine at some point in your program before the first call to
  731. fg_swchar.  You also must establish a world space coordinate system with the
  732. fg_setworld routine.
  733.  
  734.      The fg_swchar routine has three arguments.  The first argument is the
  735. character string to display.  The second argument is an integer value that
  736. specifies the number of characters in the string, including any characters
  737. used as special operators.  The third argument is an integer value that
  738. determines the position of the string relative to the graphics cursor
  739. position.  If this value is negative, the lower left corner of the first
  740. character will be at the graphics cursor position.  If it is positive, the
  741. lower right corner of the last character will be at the graphics cursor
  742. position.  If it is zero, the string will be horizontally centered at the
  743. graphics cursor position.
  744.  
  745.      The size of software characters is determined by the values passed to
  746. the fg_setsize, fg_setsizew, and fg_setratio routines.  The fg_setsize
  747. routine has a single integer argument that defines the height of software
  748. characters in screen space units, while the fg_setsizew routine has a single
  749. floating point argument that defines the height in world space units.  If
  750. neither of these routines is called, Fastgraph will use its default character
  751. height of one world space unit.  The fg_setratio routine has a single
  752. floating point argument that defines the aspect ratio for software
  753. characters.  The aspect ratio is the ratio of character width to character
  754. height.  For example, an aspect ratio of 2.0 means characters are twice as
  755. wide as they are high.  If the fg_setratio routine is not called, Fastgraph
  756. uses its default aspect ratio of 1.
  757.  
  758.      Example 7-10 displays both of the software character fonts.  The program
  759. uses the enhanced EGA graphics mode (mode 16), but it could run in any
  760. graphics mode by changing the fg_setmode argument.  After establishing the
  761. video mode, the program calls the fg_initw routine to initialize Fastgraph's
  762. world space parameters; this is required since the software character drawing
  763. routines internally use world space coordinates.  The next statement is a
  764. call to fg_setworld that establishes a world space coordinate system with
  765. 0.01 world space units per pixel.  Following this is a call to fg_setsizew
  766. that defines the character height as 0.21 world space units, or 21 pixels.
  767. Note we could have instead used the fg_setsize routine here with an integer
  768. argument of 21.
  769.  
  770.      The next part of the program draws the characters in the primary font on
  771. the upper half of the screen.  After doing this, the program draws the
  772. alternate font characters on the lower half.  In each case it does this with
  773.                                   Chapter 7:  Character Display Routines   141
  774.  
  775.  
  776. the fg_swchar routine.  By default, the string passed to fg_swchar will
  777. produce characters from the primary font.  However, you can insert a back
  778. slash character (\) in the string to toggle between the two fonts.  Don't
  779. forget the C language applies a special meaning to the back slash character
  780. within strings, so you must use two consecutive back slashes to insert a
  781. single back slash in the string.
  782.  
  783.                                 Example 7-10.
  784.  
  785.             #include <fastgraf.h>
  786.             void main(void);
  787.  
  788.             void main()
  789.             {
  790.                int old_mode;
  791.  
  792.                old_mode = fg_getmode();
  793.                fg_setmode(16);
  794.                fg_initw();
  795.                fg_setworld(0.0,6.39,0.0,3.49);
  796.                fg_setsizew(0.21);
  797.  
  798.                fg_setcolor(15);
  799.                fg_locate(0,26);
  800.                fg_text("Software characters - font 1",28);
  801.  
  802.                fg_setcolor(10);
  803.                fg_movew(0.0,3.1);
  804.                fg_swchar("ABCDEFGHIJKLMNOPQRSTUVWXYZ",26,-1);
  805.                fg_movew(0.0,2.8);
  806.                fg_swchar("abcdefghijklmnopqrstuvwxyz",26,-1);
  807.                fg_movew(0.0,2.5);
  808.                fg_swchar("0123456789",10,-1);
  809.                fg_movew(0.0,2.2);
  810.                fg_swchar("!\"#$%&'()*+,-./:;<=>?[]^`{|}~",29,-1);
  811.  
  812.                fg_setcolor(15);
  813.                fg_locate(12,26);
  814.                fg_text("Software characters - font 2",28);
  815.  
  816.                fg_setcolor(10);
  817.                fg_movew(0.0,1.4);
  818.                fg_swchar("\\ABCDEFGHIJKLMNOPRSTUWXYZ",25,-1);
  819.                fg_movew(0.0,1.1);
  820.                fg_swchar("\\abcdefghijklmnoprstuwxyz",25,-1);
  821.                fg_movew(0.0,0.4);
  822.                fg_swchar("\\012345678#$%&()*+/<=>?[]{}",27,-1);
  823.                fg_waitkey();
  824.  
  825.                fg_setmode(old_mode);
  826.                fg_reset();
  827.             }
  828.  
  829.      Example 7-10 displays all characters in each font.  If you compare the
  830. primary font strings with the alternate font strings, you'll see the
  831. alternate font contains fewer characters.  For example, the letters Q and V
  832. 142   Fastgraph User's Guide
  833.  
  834.  
  835. (either upper or lower case) have no corresponding character in the alternate
  836. font.  You might have also noticed the primary font does not support the full
  837. printable ASCII character set.  Any character in a string passed to the
  838. fg_swchar routine that does not have a corresponding character in the current
  839. font will display a blank character.
  840.  
  841.      In addition to the font change operator (the back slash character),
  842. fg_swchar recognizes three other operators.  The superscript operator is a
  843. back slash followed by a caret (\^).  It causes the next character to appear
  844. as a superscript.  Similarly, the subscript operator is a back slash followed
  845. by a lower case v (\v); it causes the next character to appear as a
  846. subscript.  The size of superscripted and subscripted characters is one half
  847. the height of the other characters.  The underline operator is the underscore
  848. character (_).  It causes all subsequent characters in the string to be
  849. underlined until another underscore character is found, or until the end of
  850. the string.  When using these operators, be sure to include them as part of
  851. the string length count passed to fg_swchar.
  852.  
  853.      Example 7-11 illustrates the use of the font selection, superscript,
  854. subscript, and underline operators with the fg_swchar routine.  Again,
  855. because the back slash character has a special meaning to the C programming
  856. language, we must use two consecutive back slashes to represent a single back
  857. slash within the string.  The program displays four strings:
  858.  
  859.                              cos˝È  + sin˝È  = 1
  860.                                      H2O
  861.                                      U232
  862.                            One word is underlined.
  863.  
  864. The theta symbol (È) in the first string is produced by displaying the
  865. character "h" in the alternate font.  Note another font selection operator
  866. (\) appears immediately after the "h" to revert to the primary font.  The
  867. first string also includes superscript operators (\^) to display the
  868. exponents in the equation.  The second string includes a single subscripted
  869. character, while the third string shows how to display three consecutive
  870. subscripted characters.  Finally, the fourth string illustrates how to
  871. underline characters.
  872.  
  873.      Note example 7-11 also uses the fg_setratio routine.  The first three
  874. strings are drawn with an aspect ratio of 2, making them twice as wide as
  875. they are high.  The fourth string is drawn with an aspect ratio of 1
  876. (Fastgraph's default aspect ratio for software characters), so the character
  877. height is the same as the character width.  Also, the strings are centered
  878. instead of left justified as in the previous example.
  879.  
  880.                                 Example 7-11.
  881.  
  882.             #include <fastgraf.h>
  883.             void main(void);
  884.  
  885.             void main()
  886.             {
  887.                int old_mode;
  888.  
  889.                old_mode = fg_getmode();
  890.                fg_setmode(16);
  891.                                   Chapter 7:  Character Display Routines   143
  892.  
  893.                fg_setcolor(10);
  894.                fg_initw();
  895.                fg_setworld(0.0,6.39,0.0,3.49);
  896.                fg_setratio(2.0);
  897.                fg_setsizew(0.21);
  898.  
  899.                fg_movew(3.2,3.0);
  900.                fg_swchar("cos\\^2\\h\\ + sin\\^2\\h\\ = 1",25,0);
  901.  
  902.                fg_movew(3.2,2.0);
  903.                fg_swchar("H\\v2O   U\\v2\\v3\\v2",18,0);
  904.  
  905.                fg_movew(3.2,1.0);
  906.                fg_setratio(1.0);
  907.                fg_swchar("One _word_ is underlined.",25,0);
  908.                fg_waitkey();
  909.  
  910.                fg_setmode(old_mode);
  911.                fg_reset();
  912.             }
  913.  
  914.      The fg_setangle routine defines the angle or orientation at which
  915. software characters are displayed.  Its only argument is a floating point
  916. value that specifies the angle, measured in degrees counterclockwise from the
  917. positive x axis.  If a program draws software characters before calling
  918. fg_setangle, Fastgraph will use its default angle of zero degrees (that is,
  919. the characters will be oriented horizontally).
  920.  
  921.      In most programs, the alternate font is not needed.  However, if you use
  922. the fg_swchar routine, Fastgraph will include the definitions of these
  923. characters in your program's data segment.  To prevent wasting this space,
  924. Fastgraph includes the fg_swtext routine.  The fg_swtext routine is same as
  925. the fg_swchar routine, except it does not include the alternate font.  Since
  926. the font selection operator does not apply when using fg_swtext, the routine
  927. simply ignores it.  You should only use fg_swtext if do not use fg_swchar.
  928. If you use both routines, your program will still work correctly, but its
  929. data segment will contain an extra copy of the primary font definitions.
  930.  
  931.      Example 7-12 demonstrates the use of the fg_setangle and fg_swtext
  932. routines.  The program draws a series of strings of the form "nnn degrees",
  933. where nnn is a multiple of 15, radiating from the screen center.  Each string
  934. appears at the specified angle.  For example, the string "15 degrees" is
  935. drawn at an angle of 15 degrees.
  936.  
  937.                                 Example 7-12.
  938.  
  939.                #include <fastgraf.h>
  940.                void main(void);
  941.  
  942.                void main()
  943.                {
  944.                   char string[24];
  945.                   int angle;
  946.                   int old_mode;
  947.  
  948.                   old_mode = fg_getmode();
  949.                   fg_setmode(16);
  950. 144   Fastgraph User's Guide
  951.  
  952.                   fg_setcolor(10);
  953.                   fg_initw();
  954.                   fg_setworld(0.0,6.39,0.0,3.49);
  955.                   fg_setsizew(0.21);
  956.  
  957.                   for (angle = 0; angle < 360; angle += 15) {
  958.                      fg_movew(3.2,1.75);
  959.                      fg_setangle((double)angle);
  960.                      sprintf(string,"     %3d degrees",angle);
  961.                      fg_swtext(string,16,-1);
  962.                      }
  963.                   fg_waitkey();
  964.  
  965.                   fg_setmode(old_mode);
  966.                   fg_reset();
  967.                }
  968.  
  969.      The final routine pertaining to software characters is fg_swlength,
  970. which returns the length of a specified string of software characters in
  971. world space units.  The length is returned as the routine's floating point
  972. function value.  The fg_swlength routine has two arguments -- a string of
  973. software characters, and an integer value specifying the number of characters
  974. in the string.  As with fg_swchar and fg_swtext, the count includes any of
  975. the special operator characters.
  976.  
  977.      Example 7-13 demonstrates a typical use of the fg_swlength routine.  The
  978. program displays the string "hello there." in light green against a gray
  979. background in the middle of the screen.  As in our previous software
  980. character examples, the program uses mode 16 and first performs the necessary
  981. initializations to use software characters.  Following this, the program uses
  982. the fg_swlength routine to compute the length in world space units of the
  983. string.  Note we have added blank characters to each end of the string passed
  984. to fg_swlength; this increases the length of the actual string and will
  985. effectively give the gray rectangle an extended border on its left and right
  986. sides.  The string length returned by fg_swlength is multiplied by 0.5,
  987. giving the distance from the middle of the screen to either side of the
  988. rectangle.  The program then uses this value to compute the minimum and
  989. maximum x coordinates passed to fg_rectw.  After drawing the gray rectangle,
  990. the program uses fg_swtext to draw the string of software characters in the
  991. middle of the screen.  It then waits for a keystroke before restoring the
  992. original video mode and screen attributes and returning to DOS.
  993.  
  994.                                 Example 7-13.
  995.  
  996.               #include <fastgraf.h>
  997.               void main(void);
  998.  
  999.               void main()
  1000.               {
  1001.                  int old_mode;
  1002.                  double half;
  1003.                  double fg_swlength();
  1004.  
  1005.                  old_mode = fg_getmode();
  1006.                  fg_setmode(16);
  1007.                  fg_initw();
  1008.  
  1009.                                   Chapter 7:  Character Display Routines   145
  1010.  
  1011.  
  1012.                  fg_setworld(0.0,6.39,0.0,3.49);
  1013.                  fg_setsizew(0.21);
  1014.  
  1015.                  fg_setcolor(7);
  1016.                  half = fg_swlength(" Hello there. ",14) * 0.5;
  1017.                  fg_rectw(3.2-half,3.2+half,1.6,1.9);
  1018.  
  1019.                  fg_setcolor(10);
  1020.                  fg_movew(3.2,1.65);
  1021.                  fg_swtext("Hello there.",12,0);
  1022.                  fg_waitkey();
  1023.  
  1024.                  fg_setmode(old_mode);
  1025.                  fg_reset();
  1026.               }
  1027.  
  1028.  
  1029.  
  1030. Bit-Mapped Characters
  1031.  
  1032.      Bit-mapped characters combine the properties of hardware and software
  1033. characters.  Like hardware characters, they are a fixed size, but they are
  1034. almost always more visually appealing.  Because they are not scalable, they
  1035. do not require floating point arithmetic, and therefore they are much faster
  1036. than software characters.
  1037.  
  1038.      Fastgraph makes no special provision for bit-mapped characters because
  1039. it treats them as if they were any other bit-mapped image.  For example, to
  1040. use a five-pixel by five-pixel bit-mapped font, you can construct characters
  1041. as shown below and then store these representations in an image array.
  1042.  
  1043.                           *       * * * *       * * * *
  1044.                         *   *     *       *   *
  1045.                       * * * * *   * * * *     *
  1046.                       *       *   *       *   *
  1047.                       *       *   * * * *       * * * *
  1048.  
  1049. The image display routines fg_drawmap and fg_drwimage, discussed in Chapter
  1050. 10, could then be used to display specific characters from the image array.
  1051. Also, the Fastgraph/Fonts  add-on product greatly simplifies adding bit-
  1052. mapped font support to Fastgraph applications.
  1053.  
  1054.  
  1055. Summary of Character Display Routines
  1056.  
  1057.      This section summarizes the functional descriptions of the Fastgraph
  1058. routines presented in this chapter.  More detailed information about these
  1059. routines, including their arguments and return values, may be found in the
  1060. Fastgraph Reference Manual.
  1061.  
  1062.      FG_CHGATTR applies the current text display attribute to a given number
  1063. of characters, starting at the text cursor position.  This routine leaves the
  1064. text cursor one column to the right of the last character changed (or the
  1065. first column of the next row if the last character is at the end of a row).
  1066. It has no effect in graphics video modes.
  1067. 146   Fastgraph User's Guide
  1068.  
  1069.  
  1070.      FG_CHGTEXT displays a string of hardware characters, starting at the
  1071. text cursor position, using the existing text display attributes.  This
  1072. routine leaves the text cursor one column to the right of the last character
  1073. displayed (or the first column of the next row if the last character is at
  1074. the end of a row).  It has no effect in graphics video modes.
  1075.  
  1076.      FG_FONTSIZE enables the 8x8, 8x14, or 8x16 ROM BIOS character font for
  1077. strings displayed with fg_print and fg_text.  This routine is meaningful only
  1078. in VGA and SVGA graphics video modes.
  1079.  
  1080.      FG_GETATTR returns the character attribute stored at the specified
  1081. position on the active video page.  It has no effect in graphics video modes.
  1082.  
  1083.      FG_GETCHAR returns the character value stored at the specified position
  1084. on the active video page.  It has no effect in graphics video modes.
  1085.  
  1086.      FG_JUSTIFY defines the horizontal and vertical justification settings
  1087. for strings displayed with the fg_print.
  1088.  
  1089.      FG_LOCATE establishes the text cursor position for the active video
  1090. page.
  1091.  
  1092.      FG_PRINT displays a string of hardware characters, relative to the
  1093. graphics cursor position, using the current color index.  By default, strings
  1094. are displayed such that the bottom row of the first character is at the
  1095. current graphics position.  On return, the graphics cursor is positioned just
  1096. to the right of the last character displayed.
  1097.  
  1098.      FG_SETANGLE defines the angle or orientation at which software
  1099. characters are displayed.  The angle is measured in degrees counterclockwise
  1100. from the positive x axis.
  1101.  
  1102.      FG_SETATTR establishes the current text display attribute in text video
  1103. modes.  This routine has no effect in graphics video modes.
  1104.  
  1105.      FG_SETCOLOR establishes the current color index (which may be a virtual
  1106. color index in graphics modes).  In text modes, the fg_setcolor routine
  1107. provides an alternate method of establishing the current text display
  1108. attribute.
  1109.  
  1110.      FG_SETRATIO defines the aspect ratio for software characters.  The
  1111. aspect ratio is the ratio of character width to character height.
  1112.  
  1113.      FG_SETSIZE defines the height of software characters in screen space
  1114. units.
  1115.  
  1116.      FG_SETSIZEW defines the height of software characters in world space
  1117. units.
  1118.  
  1119.      FG_SWCHAR displays a string of software characters using the current
  1120. color index.  The string may be left justified, centered, or right justified
  1121. relative to the graphics cursor position.  The string passed to fg_swchar may
  1122. contain special operators that allow switching between fonts, underlining,
  1123. superscripting, or subscripting.  This routine has no effect in text video
  1124. modes.
  1125.                                   Chapter 7:  Character Display Routines   147
  1126.  
  1127.  
  1128.      FG_SWLENGTH returns the length in world space units of a string of
  1129. software characters.
  1130.  
  1131.      FG_SWTEXT is a scaled down version of the fg_swchar routine.  It does
  1132. not include the alternate font character definitions and thus requires less
  1133. memory than fg_swchar.
  1134.  
  1135.      FG_TEXT displays a string of hardware characters, starting at the text
  1136. cursor position, using the current color attribute (for text modes) or color
  1137. index (for graphics modes).  This routine leaves the text cursor one column
  1138. to the right of the last character displayed (or the first column of the next
  1139. row if the last character is at the end of a row).
  1140.  
  1141.      FG_WHERE retrieves the row and column numbers of the text cursor
  1142. position.
  1143.  
  1144.      FG_XALPHA and FG_YALPHA convert screen space coordinates to character
  1145. space.
  1146.  
  1147.      FG_XCONVERT and FG_YCONVERT convert character space coordinates to
  1148. screen space.
  1149. 148   Fastgraph User's Guide
  1150.